package com.ehotting.edsta.mqbd.monitor.web.config;

import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.core.logger.MqMsgLogger;
import com.ehotting.mqbd.core.mq.batch.BatchConsumerResponseAction;
import com.ehotting.mqbd.core.mq.batch.BatchMessageRequestConsumer;
import com.ehotting.mqbd.core.mq.batch.BatchMessageResponseConsumer;
import com.ehotting.mqbd.core.mq.batch.BatchMqApplicationContext;
import com.ehotting.mqbd.core.config.MqbdCorePropertyConfig;
import com.ehotting.mqbd.core.manager.ModuleManager;
import com.ehotting.mqbd.core.manager.SolverInfoManager;
import com.ehotting.mqbd.core.sync.MqSyncFutureMapHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@DependsOn("mqbdCorePropertyConfig")
public class ConsumerActionResponseConfig {

    @Bean(name="batchMqApplicationContext")
    public BatchMqApplicationContext batchMqApplicationContext(){
        SysModuleEnum[] sysModuleArray = SysModuleEnum.values();
        BatchMqApplicationContext batchMqApplicationContext = new BatchMqApplicationContext();
        for(SysModuleEnum module : sysModuleArray){
            if(SysModuleEnum.PBC_DESIGN == module){
                continue;
            }
            MqApplicationContext mqApplicationContext = new MqApplicationContext();
            mqApplicationContext.setModule(module.getCode());
            mqApplicationContext.setLocalHost(MQDBConstants.MONITOR_HOST);
            mqApplicationContext.setConsumerHost(MQDBConstants.DISPATCHER_HOST);
            batchMqApplicationContext.getMqApplicationContextList().add(mqApplicationContext);
        };

        return batchMqApplicationContext;
    }


    @Bean(name="BatchConcurrencyConsumerResponseAction")
    public BatchConsumerResponseAction batchConcurrencyConsumerResponseAction(MqSyncFutureMapHelper<MQDataAble> mqSyncFutureMapHelper,
                                                                              BatchMqApplicationContext batchMqApplicationContext,
                                                                              ModuleManager moduleManager, MqMsgLogger mqMsgLogger){
        BatchConsumerResponseAction action = new BatchConsumerResponseAction(mqSyncFutureMapHelper,
                batchMqApplicationContext,
                TopicEnum.CONCURRENCY,
                DirectionEnum.RESPONSE,
                moduleManager,mqMsgLogger);
        return action;
    }

    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public BatchMessageResponseConsumer concurrencyRequestMessageConsumer(
            MqbdCorePropertyConfig mqbdCorePropertyConfig,
            BatchConsumerResponseAction batchConcurrencyConsumerResponseAction){
        BatchMessageResponseConsumer messageConsumer = new BatchMessageResponseConsumer(
                mqbdCorePropertyConfig.getConsumer(),
                batchConcurrencyConsumerResponseAction);
        return messageConsumer;
    }

}